// ==UserScript==
// @name Scratch Comment Blocker
// @namespace Violentmonkey Scripts
// @match *://scratch.mit.edu/*
// @grant none
// ==/UserScript==

// Customization ///////////////////////////////////////

// Should comments written by any blocked users be
// hidden?
const blockComments = true

// Should whole threads which ever had any posts by any
// blocked users be hidden?
const blockWholeThreads = true

// Blocked users ///////////////////////////////////////

// Just fill in the usernames of the users you choose
// to block here.
const blockedUsers = []

// Comment blocking ////////////////////////////////////

if (blockComments) {
  const commentsContainer = document.querySelector('#comments ul.comments')

  if (commentsContainer) {
    const observer = new MutationObserver(mutations => {
      for (const mutation of mutations) {
        for (const addedNode of mutation.addedNodes) {
          if (typeof addedNode.classList === 'undefined') continue
          if (addedNode.classList.contains('top-level-reply') === false) continue
          const topComment = addedNode
          const comments = [topComment, ...topComment.getElementsByClassName('reply')]

          for (const comment of comments) {
            const nameEl = comment.querySelector('.name a')
            if (nameEl && blockedUsers.includes(nameEl.textContent)) {
              if (blockWholeThreads) {
                topComment.remove()
                break
              } else {
                comment.remove()
              }
            }
          }
        }
      }
    })

    observer.observe(commentsContainer, {childList: true})
  }
}
